package com.ezerg.async;

import java.util.concurrent.*;

public class FutureTest {
    public static void main(String[] args) {
        //创建ExecutorService，并且通过它向线程池提交任务
        ExecutorService executorService = Executors.newCachedThreadPool();
        //向线程池提交任务
        Future future = executorService.submit(new Callable<Double>(){
            @Override
            public Double call()throws Exception{
                return 1d;
            }
        });
        //当前线程继续做其他事情
        System.out.println("do something");;
        //获取异步操作的结果
        try{
            double result = (double) future.get(1, TimeUnit.SECONDS);
            System.out.println("result=" + result);
        }catch(InterruptedException e){
            //线程被中断
            e.printStackTrace();
        }catch(ExecutionException e){
            //线程抛出异常
            e.printStackTrace();
        }catch(TimeoutException e){
            //线程超时
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }
    }
}
